﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;

public partial class UserControl_HocVien_phanContact : System.Web.UI.UserControl
{
#region "Member variables"
    private int intUserID = 0;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DANguoidung objNguoidung;
    DAHocVien objHocVien;
    DAQuyen objQuyen;
    #endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion
#region "Private methods"
    private void loadNhomL1L2(DropDownList ddlTemp)
    {       
        ListItem liTemp = new ListItem();
        liTemp.Text = "Nhóm L1-L2";
        liTemp.Value ="1";
        ddlTemp.Items.Add(liTemp);

        liTemp = new ListItem();
        liTemp.Text = "Trưởng nhóm";
        liTemp.Value = "2";
        ddlTemp.Items.Add(liTemp);

        liTemp = new ListItem();
        liTemp.Text = "Tư vấn tuyển sinh";
        liTemp.Value = "3";
        ddlTemp.Items.Add(liTemp);
    }
    private void loaddanhsachL1L2(int intCheck)
    {
        DataSet dsNguoiDungView=null;
        if (intCheck == 1)
        {
            dsNguoiDungView = objNguoidung.loadQuyenNguoiDungByQuyen(3,intUserID);           
        }
        else if(intCheck == 2)
        {
            dsNguoiDungView = objNguoidung.loadQuyenNguoiDungByQuyen(4,intUserID);
        }
        else if (intCheck == 3)
        {
            dsNguoiDungView = objNguoidung.loadQuyenNguoiDungByQuyen(5,intUserID);
        }
        this.lbL1L2.Items.Clear();
        foreach (DataRow dr in dsNguoiDungView.Tables[0].Rows)
        {
            this.lbL1L2.Items.Add(new ListItem(dr["HoTen"].ToString(), dr["ID"].ToString()));
        }
    }
    private void loaddanhsachContactchuaphan()
    {
        int intLevelID = Convert.ToInt32(this.ddlNhomL1L2.SelectedValue);
         DataSet dsContactchuaphan=null;
        if (intLevelID == 1)
        {
            dsContactchuaphan = objHocVien.loadPhanHocVienAll(1);
        }
        if (intLevelID == 2)
        {
            dsContactchuaphan = objHocVien.loadPhanHocVienAll(2);
        }
        if (intLevelID == 3)
        {
            dsContactchuaphan = objHocVien.loadPhanHocVienAll(2);
        }
      
        for (int i = 0; i < dsContactchuaphan.Tables[0].Rows.Count; i++)
        {
            this.lbContactRight.Items.Add(new ListItem(dsContactchuaphan.Tables[0].Rows[i]["HoTen"].ToString(), dsContactchuaphan.Tables[0].Rows[i]["ID"].ToString()));
        }
    }
    private void loaddanhsachContactdaphan()
    {
        int intCheck = Convert.ToInt32(this.ddlNhomL1L2.SelectedValue);
        int intnguoidung = Convert.ToInt32(this.lbL1L2.SelectedItem.Value);
        DataSet dsContactdaphan=null;
        if (intCheck == 1)
        {
            dsContactdaphan = objHocVien.loadPhanHocVienAllbyNguoidung(1,intnguoidung);
        }
        else if (intCheck == 2)
        {
            dsContactdaphan = objHocVien.loadPhanHocVienAllbyNguoidung(2,intnguoidung);
        }
        else if (intCheck == 3)
        {
            dsContactdaphan = objHocVien.loadPhanHocVienAllbyNguoidung(2, intnguoidung);
        }
        for (int i = 0; i < dsContactdaphan.Tables[0].Rows.Count; i++)
        {
            this.lbContactleft.Items.Add(new ListItem(dsContactdaphan.Tables[0].Rows[i]["HoTen"].ToString(), dsContactdaphan.Tables[0].Rows[i]["ID"].ToString()));
        }
    }
#endregion
#region "Events handle"
    //Check quyen han su dung
    private void CheckUserModule(int UserID, int ModuleID)
    {
        
    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objNguoidung = new DANguoidung(sqlConnec);
        objQuyen = new DAQuyen(sqlConnec);
        objHocVien = new DAHocVien(sqlConnec);
    }
    //Main load
    protected void Page_Load(object sender, EventArgs e)
    {
        // Put user code to initialize the page here
        intUserID = Convert.ToInt32(Session["UserID"].ToString().Trim());
        loadDataAccess();      
        if (!Page.IsPostBack)
        {
            loadNhomL1L2(this.ddlNhomL1L2);
            loaddanhsachL1L2(Convert.ToInt32(this.ddlNhomL1L2.SelectedValue));
            loaddanhsachContactchuaphan();
        }
    }
    protected void ddlNhomL1L2_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.lblMess.Text = "";      
        loaddanhsachL1L2(Convert.ToInt32(this.ddlNhomL1L2.SelectedValue));
        this.lbContactRight.Items.Clear();
        loaddanhsachContactchuaphan();
    }
    protected void lbL1L2_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.lblMess.Text = "";
        this.lbContactleft.Items.Clear();
        loaddanhsachContactdaphan();
    }
    protected void uxLevelRightBtn_Click1(object sender, ImageClickEventArgs e)
    {
        while (lbContactleft.Items.Count > 0 && lbContactleft.SelectedItem != null)
        {
            ListItem selectedItem = lbContactleft.SelectedItem;
            selectedItem.Selected = false;
            lbContactRight.Items.Add(selectedItem);
            lbContactleft.Items.Remove(selectedItem);
        }
        update_click();
    }
    protected void uxLevelRightAllBtn_Click(object sender, ImageClickEventArgs e)
    {
        foreach (ListItem ltTemp in this.lbContactleft.Items)
        {
            this.lbContactRight.Items.Add(ltTemp);
        }
        this.lbContactleft.Items.Clear();
        update_click();
    }
    protected void uxLevelLeftBtn_Click(object sender, ImageClickEventArgs e)
    {
        while (lbContactRight.Items.Count > 0 && lbContactRight.SelectedItem != null)
        {
            ListItem selectedItem = lbContactRight.SelectedItem;
            selectedItem.Selected = false;
            lbContactleft.Items.Add(selectedItem);
            lbContactRight.Items.Remove(selectedItem);
        }
        update_click();
    }
    protected void uxLevelLeftAllBtn_Click(object sender, ImageClickEventArgs e)
    {
        foreach (ListItem ltTemp in this.lbContactRight.Items)
        {
            this.lbContactleft.Items.Add(ltTemp);
        }
        this.lbContactRight.Items.Clear();
        update_click();
    }
    protected void uxLevelRightBtn_Click(object sender, ImageClickEventArgs e)
    {       
        //string strHocVienIDs = "";
        //int intNguoiBiChuyenID = 0;
        //int intNguoiNhanID = 0;
        //int intNguoiChuyenID = 0;
        //int intLevelID = 0;
        //foreach (ComponentArt.Web.UI.TreeViewNode rootTemp in this.tvNguoidungPhu.Nodes)
        //{
        //    if (rootTemp.IsSelected)
        //    {
        //        intNguoiNhanID = Convert.ToInt32(rootTemp.Value.Trim());
        //        break;
        //    }
        //}
        //foreach (ComponentArt.Web.UI.TreeViewNode rootTemp in this.tvNguoidungGoc.Nodes)
        //{
        //    intNguoiBiChuyenID = Convert.ToInt32(rootTemp.Value.Trim());
        //    if( intNguoiBiChuyenID != intNguoiNhanID)
        //    {
        //        if (rootTemp.Checked)
        //        {
        //            objHocVien.insertHistoryHocVien("", intNguoiChuyenID, intNguoiNhanID, intUserID, 0);
        //            //Response.Write("<BR> Chon tat ca nguoi dung thu " + intNguoiBiChuyenID.ToString());
        //        }
        //        else
        //        {
        //            foreach (ComponentArt.Web.UI.TreeViewNode rootTemp1 in rootTemp.Nodes)
        //            {
        //                intLevelID = Convert.ToInt32(rootTemp1.Value.Trim());
        //                if (rootTemp1.Checked)
        //                {
        //                    objHocVien.insertHistoryHocVien("", intNguoiBiChuyenID, intNguoiNhanID, intUserID, intLevelID);
        //                    //Response.Write("<BR>Chon tat ca level thu " + intLevelID.ToString());
        //                    //insert tat ca hoc vien cua Nguoi dung nay theo Level
        //                }
        //                else
        //                {
        //                    int i = 0;
        //                    strHocVienIDs = "";
        //                    foreach (ComponentArt.Web.UI.TreeViewNode rootTemp2 in rootTemp1.Nodes)
        //                    {
        //                        if (rootTemp2.Checked)
        //                        {
        //                            strHocVienIDs += rootTemp2.Value.Trim() + ",";
        //                            i++;
        //                        }
        //                    }
        //                    if (i == rootTemp1.Nodes.Count)
        //                    {
        //                        objHocVien.insertHistoryHocVien("", intNguoiBiChuyenID, intNguoiNhanID, intUserID, intLevelID);                           
        //                    }
        //                    else
        //                    {
        //                        if (!strHocVienIDs.Equals(""))
        //                        {
        //                            objHocVien.insertHistoryHocVien(strHocVienIDs, intNguoiBiChuyenID, intNguoiNhanID, intUserID, intLevelID);
        //                        }
        //                    }
        //                    //Response.Write("<BR>Chon tat contact thu " + strHocVienIDs.ToString()); 
        //                }
        //            }
        //        }
        //    }
        //}
        ////Response.Write("<BR>Chon nguoi nhan " + intNguoiNhanID.ToString());
        //loadQuyenNguoiDung(Convert.ToInt32(this.ddlNhomGoc.SelectedValue), this.tvNguoidungGoc, true);
        //loadQuyenNguoiDung(Convert.ToInt32(this.ddlNhomPhu.SelectedValue), this.tvNguoidungPhu, false);     
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string strHocVienIDs = "";
        //int intNguoiBiChuyenID = 0;      
        if (lbL1L2.SelectedItem != null)
        {
            int intNguoiNhanID = Convert.ToInt32(this.lbL1L2.SelectedValue);
            int intLevelID = Convert.ToInt32(this.ddlNhomL1L2.SelectedValue);
            strHocVienIDs = "";
            for (int i = 0; i < this.lbContactleft.Items.Count; i++)
            {
                strHocVienIDs += this.lbContactleft.Items[i].Value + ",";
            }
            //if(intLevelID==1)
            objHocVien.insertHistoryHocVien(strHocVienIDs, intNguoiNhanID, intUserID, intLevelID);
            this.lblMess.Text = "Cập nhật thành công!";
        }
        else
        {
            this.lblMess.Text = "Bạn phải chọn Danh sách cán bộ!";
        }
    }
    private void update_click()
    {
        string strHocVienIDs = "";
        //int intNguoiBiChuyenID = 0;      
        if (lbL1L2.SelectedItem != null)
        {
            int intNguoiNhanID = Convert.ToInt32(this.lbL1L2.SelectedValue);
            int intLevelID = Convert.ToInt32(this.ddlNhomL1L2.SelectedValue);
            strHocVienIDs = "";
            for (int i = 0; i < this.lbContactleft.Items.Count; i++)
            {
                strHocVienIDs += this.lbContactleft.Items[i].Value + ",";
            }
            //if(intLevelID==1)
            objHocVien.insertHistoryHocVien(strHocVienIDs, intNguoiNhanID, intUserID, intLevelID);
            this.lblMess.Text = "Cập nhật thành công!";
        }
        else
        {
            this.lblMess.Text = "Bạn phải chọn Danh sách cán bộ!";
        }
    }
#endregion

}
